import re
#
# #列表
# list = re.findall(r"\d+", "ladaldad:2138123")
# print(list)
#
# #迭代器
# it = re.finditer(r"\d+", "dadad21313")
# for i in it:
#     print(i.group())
#
# #找到一个就返回
# s = re.search(r"\d+", "eqw133eqe13")
# print(s.group())
#
# #默认从头匹配，加^
# m = re.match(r"\d+", "eqw133eqe13")
# print(m)
#
# #预加载正则
# obj = re.compile(r"\d+")
# ret = obj.finditer("qeq121")
# for i in ret:
#     print(i.group())
#
# s = """
#     <div class='jj'><spqn id='1'>s当前sq</span></div>
#     <div class='jwj'><spqn id='332'>s侵权sq</span></div>
#     <div class='jqj'><spqn id='32'>sq五分q</span></div>
#     <div class='jcj'><spqn id='34'>sq几天后q</span></div>
#     <div class='jQj'><spqn id='7'>sqs二分</span></div>
# """
#
# obj = re.compile(r"<div class='.*?'><spqn id='(?P<id>\d+)'>(?P<QWQ>.*?)</span></div>")
#
# result = obj.finditer(s)
# for it in result:
#     print(it.group("id"))
#     print(it.group("QWQ"))

# import requests
# import csv
# url = "https://movie.douban.com/top250"
# heards = {
#     "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.88 Safari/537.36"
# }
# resp = requests.get(url, headers=heards)
# t = resp.text
#
# obj = re.compile(r'<li>.*?<div class="item">.*?<span class="title">(?P<name>.*?)</span>'
#                  r'.*?<br>(?P<year>.*?)&nbsp'
#                  r'.*?<span class="rating_num" property="v:average">(?P<score>.*?)</span>'
#                  r'.*?<span>(?P<num>.*?)人评价</span>', re.S)
#
# result = obj.finditer(t)
# f = open("data2.csv", mode="w", encoding="utf-8")
# csv_w = csv.writer(f)
#
# for it in result:
#     print(it.group("name"))
#     print(it.group("score"))
#     print(it.group("num"))
#     print(it.group("year").strip())
#     # dic = it.groupdict()
#     # dic['year'] = dic['year'].strip()
#     # csv_w.writerow(dic.values())
# resp.close()
'''
爬取电影天堂动漫数据———正则表达式
'''

import requests
import re

url = "https://www.dygod.net/"
heards = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.88 Safari/537.36"
}

resp = requests.get(url=url, headers=heards)
resp.encoding = "gbk"
t = resp.text

obj = re.compile(r"最新动漫资源.*?<ul>.*?(?P<ul>.*?)</ul>", re.S)
obj2 = re.compile(r"<a href='(?P<herf>.*?)'", re.S)
obj3 = re.compile(r"◎片　　名(?P<name>.*?)<br />.*?◎简　　介<br />(?P<dj>.*?)。", re.S)

result = obj.finditer(t)
child_href = []
for it in result:
    for itt in obj2.finditer(it.group('ul')):
        child_href.append(url + itt.group('herf'))
        # print(itt.group('herf'))

for c in child_href:
    resp1 = requests.get(url=c, headers=heards)
    resp1.encoding = "gbk"
    t1 = resp1.text

    r = obj3.finditer(t1)
    for it1 in r:
        print(it1.group("name"))
        print(it1.group("dj"))
